//
//  ZClubSettingVC.m
//  WeClubs
//
//  Created by Zed Link on 17/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZClubSettingVC.h"
#import "ZClubSettingCell.h"
#import "ZClubEditVC.h"
#import "ZClubCreateVC.h"
#import "ZOwnerClubModel.h"

NSString * const ClubSettingCellReuseID = @"ZClubSettingCellReuseID";

@interface ZClubSettingVC () <UITableViewDelegate, UITableViewDataSource>

/** 主界面 */
@property (weak, nonatomic) UITableView *mainView;

/** 数据源 */
@property (strong, nonatomic) NSArray <ZOwnerClubModel *> *ownerClubs;

@end

@implementation ZClubSettingVC

@synthesize ownerClubs = _ownerClubs;

#pragma mark - About Navigation

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"组织设置";
    self.navigationItem.rightBarButtonItem = [self zNavItemWithTitle:@"创建组织" andSelector:@selector(createClubAction)];


}

- (void)createClubAction
{
    ZClubCreateVC *createVC = [ZClubCreateVC zNew];
    [self.navigationController pushViewController:createVC animated:YES];
}

#pragma mark - View Life Cycle

- (void)zLoadSubview
{
    [super zLoadSubview];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // ========== 主界面 ==========
    UITableView *mainView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    mainView.delegate = self;
    mainView.dataSource = self;
    [mainView setBackgroundColor:GENERAL_VIEW_BG_COLOR];
    
    mainView.separatorStyle = UITableViewCellSeparatorStyleNone;
    mainView.contentInset = UIEdgeInsetsMake(16, 0, 0, 0);
    mainView.rowHeight = 68 + 16;
    
    [mainView registerNib:[UINib nibWithNibName:@"ZClubSettingCell" bundle:nil] forCellReuseIdentifier:ClubSettingCellReuseID];
    
    self.mainView = mainView;
    [self.view addSubview:mainView];
    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(64, 0, 0, 0));
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self loadDataFormNetWork];
}

- (void)loadDataFormNetWork{
    
    NSDictionary *dataPara = @{
                               @"user_id" : @([ZUserInfo sharedInstance].userId),
                               @"token" : [ZUserInfo sharedInstance].token,
                               @"sponsor_id" : @([ZUserInfo sharedInstance].userId),
                               };
    [HTTPRequest POST:kManager_club_getMyClubURL parameter:dataPara success:^(id resposeObject) {
        NSArray *temp = [resposeObject[@"data"] objectForKey:@"club"];
        NSMutableArray *ownerClubs = [NSMutableArray array];
        for (NSDictionary *dict in temp) {
            ZOwnerClubModel *model = [[ZOwnerClubModel alloc] initWithDictionary:dict];
            [ownerClubs addObject:model];
        }
        self.ownerClubs = [ownerClubs copy];
    } failure:^(NSError *error) {
        // Todo: 获取拥有组织列表失败
        [MBProgressHUD zAlertToastMessage:@"获取组织列表失败"];
    }];


}
#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.ownerClubs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZOwnerClubModel *model = [self.ownerClubs objectAtIndex:indexPath.row];
    
    ZClubSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ClubSettingCellReuseID];
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZOwnerClubModel *model = [self.ownerClubs objectAtIndex:indexPath.row];
    
    ZClubEditVC *editVC = [ZClubEditVC zNew];
    editVC.model = model;
    [self.navigationController pushViewController:editVC animated:YES];
}

#pragma mark - Getter & Setter

- (NSArray<ZOwnerClubModel *> *)ownerClubs
{
    if (_ownerClubs == nil) {
        _ownerClubs = [NSArray array];
    }
    return _ownerClubs;
}

- (void)setOwnerClubs:(NSArray<ZOwnerClubModel *> *)ownerClubs
{
    _ownerClubs = ownerClubs;
    [self.mainView reloadData];
}

@end
